﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Knihotoc_WP8.Settings;
using System.Device.Location;

namespace Knihotoc_WP8.Pages
{
    public partial class MapPage : PhoneApplicationPage
    {
        public MapPage()
        {
            this.InitializeComponent();

            (ApplicationBar.Buttons[0] as ApplicationBarIconButton).Text = Localization.LocalizedStrings.Get("CurrentLocationButtonLabel");

            currentPositionPushpin.Template = (ControlTemplate)(App.Current.Resources["PushpinCurrentPosition"]);
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            this.currentPositionPushpin.Location = CurrentSettings.Instance.CurrentPosition;
            CurrentSettings.Instance.CurrentPositionChanged += this.CurrentSettings_CurrentPositionChanged;

            if (NavigationContext.QueryString.ContainsKey("Latitude")
                && NavigationContext.QueryString.ContainsKey("Longitude"))
            {
                this.map.Center = new GeoCoordinate(float.Parse(NavigationContext.QueryString["Latitude"]),
                                                    float.Parse(NavigationContext.QueryString["Longitude"]));
            }
            else
            {
                this.map.Center = this.currentPositionPushpin.Location;
            }

            // Depends on where we navigate from - MainPage vs. BookDetailPage... modify
            this.map.ZoomLevel = 17.0;
            //this.UpdatePrecision(CurrentSettings.Instance.CurrentPosition);
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            CurrentSettings.Instance.CurrentPositionChanged -= this.CurrentSettings_CurrentPositionChanged;
            NavigationService.Navigate(new Uri("/Pages/MainPage.xaml", UriKind.Relative));
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            //CurrentSettings.Instance.CurrentLocationWatcherStop();
            //CurrentSettings.Instance.CurrentPositionChanged -= this.CurrentSettings_CurrentPositionChanged;
        }

        private void CurrentSettings_CurrentPositionChanged(object sender, System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate> e)
        {
            this.currentPositionPushpin.Location = e.Position.Location;
            this.UpdatePrecision(e.Position.Location);
        }

        private void UpdatePrecision(GeoCoordinate location)
        {
            precision.Visibility = Visibility.Collapsed;
            if (location == null)
                return;

            precision.Visibility = Visibility.Visible;
            precision.Locations = new Microsoft.Phone.Controls.Maps.LocationCollection();

            var earthRadius = 6371;
            var lat = location.Latitude * Math.PI / 180.0; //radians 
            var lon = location.Longitude * Math.PI / 180.0; //radians 
            var d = location.HorizontalAccuracy / 1000 / earthRadius; // d = angular distance covered on earths surface 

            for (int x = 0; x <= 360; x++)
            {
                var brng = x * Math.PI / 180.0; //radians 
                var latRadians = Math.Asin(Math.Sin(lat) * Math.Cos(d) + Math.Cos(lat) * Math.Sin(d) * Math.Cos(brng));
                var lngRadians = lon + Math.Atan2(Math.Sin(brng) * Math.Sin(d) * Math.Cos(lat), Math.Cos(d) - Math.Sin(lat) * Math.Sin(latRadians));

                var pt = new GeoCoordinate(180.0 * latRadians / Math.PI, 180.0 * lngRadians / Math.PI);
                precision.Locations.Add(pt);
            }
        }

        private void currentLocationButton_Click_1(object sender, EventArgs e)
        {
            this.map.Center = this.currentPositionPushpin.Location;
            this.map.ZoomLevel = DefaultSettings.Instance.MapZoomLevel;
        }

        private void map_ViewChangeEnd(object sender, Microsoft.Phone.Controls.Maps.MapEventArgs e)
        {
            GeoCoordinate coordinateUpperLeft = map.ViewportPointToLocation(new Point(0, 0));
            GeoCoordinate coordinateBottomRight = map.ViewportPointToLocation(new Point(map.ViewportSize.Width, map.ViewportSize.Height));            
        }
                
        void newPlacePushpin_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //NavigationService.Navigate(new Uri("/Pages/CommonPlaceDetailPage.xaml?PlaceID=" + (sender as Pushpin).Name, UriKind.Relative));
        }

        void newBookPushpin_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //NavigationService.Navigate(new Uri("/Pages/BookDetailPage.xaml?BookID=" + (sender as Pushpin).Name, UriKind.Relative));
        }
    }
}